<template>
    <div>
        <basic-container>
            <avue-crud
                    :data="list"
                    :option="tableOption"
                    ref="crud"
                    v-model="form"
                    @on-load="getList"
                    :table-loading="listLoading"
                    :page="page"
                    @search-change="handleFilter"
                    @refresh-change="handleRefreshChange"
            >
                <template slot-scope="scope" slot="menuBtn">
                    <el-dropdown-item
                            divided
                            v-if="permissions.sys_activiti_process_manage"
                            @click.native="handlePic(scope.row,scope.index)"
                    >流程图
                    </el-dropdown-item>
                    <el-dropdown-item
                            divided
                            v-if="permissions.sys_activiti_process_manage && scope.row.suspend"
                            @click.native="handleStatus(scope.row,'active')"
                    >激活
                    </el-dropdown-item>

                    <el-dropdown-item
                            divided
                            v-if="permissions.sys_activiti_process_manage && !scope.row.suspend"
                            @click.native="handleStatus(scope.row,'suspend')"
                    >失效
                    </el-dropdown-item>

                    <el-dropdown-item
                            divided
                            v-if="permissions.sys_activiti_process_manage"
                            @click.native="deletes(scope.row,'suspend')"
                    >删除
                    </el-dropdown-item>
                </template>
            </avue-crud>
        </basic-container>
        <el-dialog title="流程图" :visible.sync="showPicDialog">
            <img :src="actPicUrl" width="100%">
        </el-dialog>
    </div>
</template>

<script>
    import {listPage,updateProcess,deleteProcess} from "@/api/activiti/process";
    import {tableOption} from '@/const/activiti/process';
    import {mapGetters} from "vuex";

    export default {
        data() {      return {
            showPicDialog: false,
            actPicUrl: "",
            list: [],
            page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20 // 每页显示多少条
            },
            listLoading: false,
            form: {},
            tableOption: tableOption
        };
        },
        computed: {
            ...mapGetters(["permissions"])
        },
        methods: {
            //获取分页数据
            getList(page, params) {
                this.listLoading = true;
                listPage(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.list = response.data.data.records
                    this.page.total = response.data.data.total
                    this.listLoading = false;
                })
            },
            handlePic(row) {
                this.actPicUrl =
                    `/activiti/process/resource?processDefinitionId=` +
                    row.processDefinitionId +
                    "&resourceType=image";
                this.showPicDialog = true;
            },
            handleStatus(row, type) {
                var _this = this;
                this.$confirm(
                    '是否确认操作ID为"' + row.processDefinitionId + '"的流程?',
                    "警告",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }
                ).then(function () {
                    return updateProcess({'processDefinitionId':row.processDefinitionId,'status':type});
                }).then(() => {
                    this.getList(this.page);
                    _this.$message({
                        showClose: true,
                        message: "操作成功",
                        type: "success"
                    });
                    this.getList(this.page);
                })
            },
            deletes(row,index){
                this.$confirm(
                    "此操作将永久删除, 是否继续?",
                    "提示",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }
                ).then(() =>{
                    deleteProcess(row.deploymentId).then(() =>{
                        this.list.splice(index, 1);
                        this.$notify({
                            title: "成功",
                            message: "删除成功",
                            type: "success",
                            duration: 2000
                        });
                        this.getList(this.page);
                    });
                })
            },
            handleFilter(param) {
                this.page.page = 1;
                this.getList(this.page, param);
            },
            handleRefreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>
